﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace GoogleMapsAPI
{
    public partial class frmcapnhat : Form
    {
        frmMain frmmain;
        Data old;
        string lat;
        string lng;

        public frmcapnhat(frmMain main, Data _old, string _lat, string _lng)
        {
            frmmain = main;
            InitializeComponent();
            old = _old;
            lat = _lat;
            lng = _lng;
        }

        private void btnthemloai_Click(object sender, EventArgs e)
        {
            frmloaidiadiem frm = new frmloaidiadiem(frmmain, this);
            frm.ShowDialog();
        }

        private void frmcapnhat_Load(object sender, EventArgs e)
        {
            loadcombo();
            txttendiadiem.Text = old.Name;
            cbloaidd.SelectedText = old.Group;
            textBox1.Text = old.Lat;
            textBox2.Text = old.Lng;
        }

        public void clearcombo()
        {
            cbloaidd.Items.Clear();
        }

        public void loadcombo()
        {
            List<XElement> list = Data.ReadData();
            foreach (XElement element in list)
            {
                string temp = element.Attribute("name").Value;
                cbloaidd.Items.Add(temp);
            }
        }

        private void btnthemdd_Click(object sender, EventArgs e)
        {
            try
            {
                Data newplace = new Data();
                newplace.Name = txttendiadiem.Text;
                newplace.Group = cbloaidd.Text;
                newplace.Lng = textBox2.Text;
                newplace.Lat = textBox1.Text;
                Data.UpdatePlace(old, newplace);
                MessageBox.Show("Succeed.");
                frmmain.clearTree();
                frmmain.LoadTreeView();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Vị trí Marker")
            {
                textBox1.Text = lat;
                textBox2.Text = lng;
                button1.Text = "Vị trí cũ";
            }
            else
            {
                textBox1.Text = old.Lat;
                textBox2.Text = old.Lng;
                button1.Text = "Vị trí Marker";
            }
        }
    }
}
